package com.mccraywebtech.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mccraywebtech.models.User;
import com.mccraywebtech.services.GenericService;


@Controller("loginController")
public class LoginController {
	@Autowired
	@Qualifier("loginService")
	private GenericService<User> loginService;

	/**
	 * @return the loginService
	 */
	public GenericService<User> getLoginService() {
		return loginService;
	}

	/**
	 * @param loginService the loginService to set
	 */
	public void setLoginService(GenericService<User> loginService) {
		this.loginService = loginService;
	}

	@RequestMapping("/login")
	public String startLogin(Map<String, Object> map) {
		map.put("login", new User());
		return "login";
	}
	
	@RequestMapping("/admin")
	public String startAdmin(Map<String, Object> map) {
		map.put("userList", loginService.getAllObjects());
		return "admin";
	}

	@RequestMapping(value = "/loginUser", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("login") User login,
			BindingResult result) {

		User compare = loginService.getByUsername(login.getUsername());
		if (compare == null
				|| !compare.getPassword().equals(login.getPassword())) {
			return "invalidPassword";
		} else {
			if(compare.isAdmin()){
				return "redirect:/admin";
			}else{
				return "redirect:/index";
			}
		}
	}

	@RequestMapping("/createUser")
	public String createUser(Map<String, Object> map) {
		return createUserString(map, "createUser");
	}
	@RequestMapping("/createUserAdmin")
	public String createUserAdmin(Map<String, Object> map) {
		return createUserString(map, "createUserAdmin");
	}
	
	private String createUserString(Map<String, Object> map, String url){
		map.put("newUser", new User());
		return url;
	}
	
	@RequestMapping("/deleteUser/{userId}")
	public String deleteContact(@PathVariable("userId") Integer userId) {

		loginService.deleteObject(userId);
		return "redirect:/admin";
	}

	@RequestMapping(value = "/newUser", method = RequestMethod.POST)
	public String addUser(@ModelAttribute("newUser") User newUser,
			BindingResult result) {

		if (newUser.getPassword().equals(newUser.getReTypePassword())) {
			loginService.saveObject(newUser);
			return "redirect:/login";
		} else {
			return "invalidCreate";
		}
	}
	
	@RequestMapping(value = "/newUserAdmin", method = RequestMethod.POST)
	public String addUserAdmin(@ModelAttribute("newUser") User newUser,
			BindingResult result) {

		if (newUser.getPassword().equals(newUser.getReTypePassword())) {
			loginService.saveObject(newUser);
			return "redirect:/admin";
		} else {
			return "invalidCreate";
		}
	}
}
